package com.jk.cxh.server.impl;

import com.jk.CinemaBean;
import com.jk.MovieBean;
import com.jk.MovieHallBean;
import com.jk.ProgramBean;
import com.jk.cxh.dao.PlanMovieDao;
import com.jk.cxh.server.PlanMovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author 程旭航
 * @Create 2021/5/19 - 16:31
 * @Description
 */
@Service
public class PlanMovieServiceImpl implements PlanMovieService {

    @Autowired
    private PlanMovieDao cxhMovieDao;

    @Autowired
    private HttpSession session;

    SimpleDateFormat ff = new SimpleDateFormat("HH:mm");



    /**
     * @Author:程旭航
     * @Description: 影厅数量
     * @Date: 2021/5/19 20:15
     * @param id
     * @Return: int
     **/
    @Override
    public List<MovieHallBean> hallCount(Integer id) {
        return cxhMovieDao.hallCount(id);
    }

    /**
     * @Author:程旭航
     * @Description: 新增电影计划
     * @Date: 2021/5/20 12:09
     * @param program
     * @param session
     * @Return: void
     **/
    @Override
    public String addplay(ProgramBean program, HttpSession session) throws ParseException {
        Integer movieid = program.getMovieid();
        CinemaBean cinema = (CinemaBean) session.getAttribute("user");
          //查询电影
        MovieBean bran = cxhMovieDao.queryMovid22(movieid);
        program.setMovieName(bran.getMovieName());
        String moviedate = bran.getMoviedate();
        String s = moviedate.replace("分钟", "");
        Integer integer = Integer.valueOf(s)+30;
        String time = program.getPlayStartTime();

            Date parse = ff.parse(time);
       long sd= (parse.getTime()+integer*60*1000);
        Date date = new Date(sd);
        String format = ff.format(date);
        program.setPlayEndTime(format);

        MovieBean bean1= cxhMovieDao.querymov(program); //之前
        MovieBean bean2= cxhMovieDao.querymov2(program);//之后

        if(bean1 != null && bean2 != null){
            return "上一场放映的是《"+bean1.getMovieName()+"》放映时间是"+bean1.getPlayStartTime()+"—"+bean1.getPlayEndTime()+"  下一场放映的是《" + bean2.getMovieName() + "》放映时间是" + bean2.getPlayStartTime() + "—" + bean2.getPlayEndTime()+"请另选时间安排。";
        }
        else if(bean1 != null || bean2 != null){
            if (bean1 != null){
                return "上一场放映的是《"+bean1.getMovieName()+"》放映时间是"+bean1.getPlayStartTime()+"—"+bean1.getPlayEndTime()+"请另选时间安排。";
            }
            if (bean2 != null) {
                return "下一场放映的是《" + bean2.getMovieName() + "》放映时间是" + bean2.getPlayStartTime() + "—" + bean2.getPlayEndTime()+"请另选时间安排。";
            }
        }
        cxhMovieDao.addplay(program);
        MovieBean movie = new MovieBean();
        movie.setMovieId(movieid);
        movie.setMoviePrice(program.getMoviePrice());
        cxhMovieDao.addCinemaMovie(cinema,movie);//新增影院影片中间表
        return "安排放映成功";

    }

    /**
     * @Author:程旭航
     * @Description: 查询电影id
     * @Date: 2021/5/20 12:10
     * @param name
     * @Return: java.lang.Integer
     *
     * @return*/
    @Override
    public MovieBean findMovies(String name) {
        return cxhMovieDao.findMovies(name);
    }

    /**
     * @Author:程旭航
     * @Description: 查询计划
     * @Date: 2021/6/7 19:09
     * @param num
     * @param id
     * @param hallId
     * @Return: java.util.List<com.jk.ProgramBean>
     **/
    @Override
    public List<ProgramBean> queryPlan(String num, Integer id,Integer hallId)  {
        return cxhMovieDao.queryPlan( num,  id,hallId);
    }

    @Override
    public List<MovieBean> queryMovie( ) {
        return cxhMovieDao.queryMovie( );
    }

    @Override
    public Double queryPrice(Integer cinemaId, Integer movieId) {

        return cxhMovieDao.queryPrice( cinemaId,  movieId);
    }

    @Override
    public String endDate(Integer movid, String date) throws ParseException {
        MovieBean bran = cxhMovieDao.queryMovid22(movid);
        String moviedate = bran.getMoviedate();
        String s = moviedate.replace("分钟", "");
        Integer integer = Integer.valueOf(s)+30;

        Date parse = ff.parse(date);
        long sd= (parse.getTime()+integer*60*1000);
        Date ss = new Date(sd);
        String format = ff.format(ss);
        return format;
    }

}
